package com.wp.rcm.controller;
import com.wp.rcm.pojo.Chance;
import com.wp.rcm.pojo.User;
import com.wp.rcm.service.ChanceService;
import com.wp.rcm.service.UserService;
import com.wp.rcm.util.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chance")
public class ChanceController {
    @Resource
    private ChanceService chanceService;

    @Resource
    private UserService userService;

    @GetMapping("/list")
    public String findAll(Model model, @RequestParam(required = false)String custName,
                          @RequestParam(required = false)String chcDesc,Integer currPageNo){
        Integer page1 = currPageNo==null?1:currPageNo;

        Page page = new Page();
        page.setTotal(chanceService.count(custName,chcDesc)); // 数据总条数
        if (page1<1){
            page1=1;
            model.addAttribute("message","当前已是最顶一页");
        }
        if (page1>page.getPages()){
            page1=page.getPages();
            model.addAttribute("message","当前已是最后一页");
        }
        page.setCurrent(page1); // 设置偏移量

        // 存储数据
        List<Chance> all = chanceService.findAll(custName, chcDesc, page.getOffset(), page.getPageSize());
        page.setList(all);

        model.addAttribute("chancePage",page);


        return "chance/list";
    }

    // 跳转添加页面
    @RequestMapping("/toAdd")
    public String toAdd(Model model){

        List<User> user = userService.findAll();

        model.addAttribute("users",user);

        return "chance/add";
    }

    // 添加信息
    @PostMapping("/add")
    @RequiresPermissions("销售机会列表")
    public String add(Chance chance) throws ParseException {

//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//
//        Date date = format.parse(chcDueDate); //将字符串转化为Date类型
//
//        //执行插入操作，将Date类型的日期数据插入数据库中
//        chance.setChcDueDate(date);
        int add = chanceService.insert(chance);
        if (add>0){
            return "redirect:/chance/list";
        }
        return "chance/add";
    }

    // 删除操作
    @GetMapping("/del/{chcId}")
    @ResponseBody
    public Map delete(@PathVariable(name = "chcId") Integer chcId){

        Map<String,Object> map = new HashMap<>();

        int del = chanceService.delete(chcId);
//        if (del>0){
//            map.put("delResult","true");
//        }else {
//            map.put("delResult","false");
//        }
        System.out.println(del);
        return map;
    }

    // 修改数据
    @RequestMapping("/toedit")
    public String toedit(Integer chcId,Model model){

        System.out.println("-------------"+chcId);

        Chance chance = chanceService.findByChcId(chcId);
        model.addAttribute("chance",chance);


        List<User> user = userService.findAll();
        model.addAttribute("users",user);

        return "chance/edit";
    }

    @PostMapping("/edit")
    public String edit(Chance chance){

        int upd = chanceService.update(chance);

        if (upd>0){
            return "redirect:/chance/list";
        }
        return "redirect:chance/toedit";
    }
}
